#!/usr/bin/perl
use Getopt::Std;
#perl perl_create_sdrf.pl -d ./GBM_sdrf -o GBM.sdrf
#use strict;
#getopt("d:o", \%args);
my %args;
getopt("d:o", \%args);
#sdrf file directory
my $maindir = $args{d};
#output combined sdrf file
my $output = $args{o};
chomp($maindir);
chomp($output);
open(WRBUFF,">$output");
open(BUFF,"ls $maindir |");
$k =1;
while($line = <BUFF>)
{
	chomp($line);
	#print $line."\n";
	$BUF = "BUF$k";
	open($BUF,"$maindir/$line");
	$liney = <$BUF>;
	while(<$BUF>)
	{
		chomp($_);
		#print $liney."\n";
		@array = split(/\t/,$_);
		if($array[1] =~ m/^TCGA/)
		{
			$temp = $array[1];
		}
		elsif($array[0] =~ m/^TCGA/)
		{
			$temp = $array[0];
		}
		if($array[11] =~ m/CEL$/)
                {
                        $temp1 = $array[11];
                }
                elsif($array[12] =~ m/CEL$/)
                {
                        $temp1 = $array[12];
                }
		elsif($array[13] =~ m/CEL$/)
		{
			$temp1 = $array[13];
		}

		if(!exists($hash{"$temp"}))
		{
			$hash{"$temp"} =$temp1;
		}
		#if($temp eq "TCGA-13-1489-02A-01D-2069-01")
		#{
		#	print "$array[13]\n";
		#}
	}
	#last;
	$k++;
}
while(($key,$value) = each %hash)
{
	if($key =~ m/^TCGA/)
	{
		print WRBUFF $key."\t$value\n";
	}
}
